"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserResourceDao = void 0;
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const UserResourceEntity_1 = require("../entity/UserResourceEntity");
class UserResourceDao extends DaoBase_1.DaoBase {
    static getRepository() {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepositoryBase(UserResourceEntity_1.UserResourceEntity, this.getGameDbName());
            return repository;
        });
    }
    static update_user(entity) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result;
            if (!repository.hasId(entity)) {
                result = yield repository.save(entity);
            }
            else {
                let updateResult = yield repository.update({ userid: entity.userid }, entity);
                // { generatedMaps: [], raw: [], affected: 1 }
                if (updateResult && updateResult.affected > 0) {
                    result = entity;
                }
            }
            return result;
        });
    }
    static getResource(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let entity = yield repository.findOneBy({
                userid: userid
            });
            if (!entity) {
                entity = new UserResourceEntity_1.UserResourceEntity();
                entity.userid = userid;
                yield repository.save(entity);
            }
            return entity;
        });
    }
}
exports.UserResourceDao = UserResourceDao;
UserResourceDao.Tb_Name = "t_resources";
